//
//  OrderTimeView.m
//  SDSY
//
//  Created by 蓝布鲁 on 2017/4/11.
//  Copyright (c) 2017 蓝布鲁. All rights reserved.
//

#import "OrderTimeView.h"
#import "TeacherOrderTimeCell.h"
#import "UserInfoModel.h"
#import "EvalutionTeacherController.h"
#import "ViewEvalutionTeacherController.h"

@implementation OrderTimeView

- (instancetype)initWithFrame:(CGRect)frame {
    if(self = [super initWithFrame:frame]) {
        [self setupView];
    }
    return self;
}

#pragma mark - 自定义方法
-(void)setupView {
    // 初始化tableView
    self.tableView = [[UITableView alloc] initWithFrame:self.frame];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self addSubview:self.tableView];
}

#pragma mark - 网络请求
// 选择预约类型
- (void)chooseOrderTypeWithComplete:(void (^)(NSInteger type))complete {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请选择预约问题类型" preferredStyle:UIAlertControllerStyleActionSheet];
    for (OrderTypeModel *model in self.types) {
        UIAlertAction *action = [UIAlertAction actionWithTitle:model.type style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            complete(model.oid);
        }];
        [alert addAction:action];
    }
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alert addAction:action];
    [self.viewController presentViewController:alert animated:true completion:nil];
}

// 预约导师/取消预约导师
-(void)updateOrderTeacherWithModel:(TeacherOrderTimeModel *)model type:(NSInteger)type{
    [SVProgressHUD show];
    // 请求参数
    NSDictionary *parameters = @{@"s_id": [UserInfoModel shareInstance].stu_id, @"yy_id": @(model.yyid), @"code": @(model.code), @"type": @(type)};
    
    [[NetworkTool shareInstance] requireMethodType:POSTType
                                         URLString:@"SDSY/appointTeacher"
                                        parameters:parameters
                                           success:^(NSDictionary *respondDictionary) {
                                               model.code = [respondDictionary[@"code"] intValue];
                                               [self.tableView reloadData];
                                               [SVProgressHUD dismiss];
                                               
                                               if(model.code == 4) {
                                                   // 预约成功
                                                   [SVProgressHUD showSuccessWithStatus:@"预约导师成功"];
                                               }else if(model.code == 3) {
                                                   // 取消预约成功
                                                   [SVProgressHUD showSuccessWithStatus:@"取消预约导师成功"];
                                               }else{
                                                   [SVProgressHUD showfailed];
                                               }
                                           } failure:^(NSError *error) {
                                               
                                               [SVProgressHUD dismiss];
                                               [SVProgressHUD showErrorWithStatus:@"预约失败"];
                                           }];
}

// 根据导师+时间评价
-(void)evalutionTeacherWithTeacherID:(NSString *)teacherID yyid:(NSInteger)yyid{
    EvalutionTeacherController *controller = [[EvalutionTeacherController alloc] init];
    controller.teacherID = teacherID;
    controller.yyid = yyid;
    [self.viewController.navigationController pushViewController:controller animated:true];
}

// 查看已评价的详情
-(void)viewEvalutionWithTeacherID:(NSString *)teacherID yyid:(NSInteger)yyid {
    ViewEvalutionTeacherController *controller = [[ViewEvalutionTeacherController alloc] init];
    controller.teacherID = teacherID;
    controller.yyid = yyid;
    [self.viewController.navigationController pushViewController:controller animated:true];
}

// 查看取消原因
- (void)showCancelReasonWithyyid:(NSInteger)yyid {
    [SVProgressHUD show];
    [[NetworkTool shareInstance] requireMethodType:POSTType
                                         URLString:@"SDSY/getReason"
                                        parameters:@{@"s_id": [UserInfoModel shareInstance].stu_id, @"yy_id": @(yyid)}
                                           success:^(NSDictionary *respondDictionary) {
                                               [SVProgressHUD dismiss];
                                               
                                               NSString *reason = respondDictionary[@"reason"];
                                               if (reason == nil || [reason isEqualToString: @""]) {
                                                   reason = @"导师没有说明原因";
                                               }else {
                                                   reason = [NSString stringWithFormat:@"取消原因: %@",reason];
                                               }
                                               [SVProgressHUD showInfoWithStatus:reason];
                                           } failure:^(NSError *error) {
                                               
                                               [SVProgressHUD dismiss];
                                               [SVProgressHUD showErrorWithStatus:@"查看取消原因失败"];
                                           }];
}

#pragma mark - 重写方法
- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];
    self.tableView.frame = self.bounds;
}

- (void)setTimes:(NSArray<TeacherOrderTimeModel *> *)times {
    _times = times;
    [self.tableView reloadData];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.times.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TeacherOrderTimeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ORDCELL"];

    if(cell == nil) {
        cell = [[TeacherOrderTimeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ORDCELL"];
    }
    cell.model = self.times[indexPath.row];
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:tableView];


    if (indexPath.section == 0) {// 对预约进行操作
        // 取出预约时间模型
        TeacherOrderTimeModel *model = self.times[indexPath.row];
        
        // 判断评价导师或是预约\取消导师
        if(model.code == 3) {
            // 预约(3),先选择预约导师的咨询类型
            [self chooseOrderTypeWithComplete:^(NSInteger type) {
                [self updateOrderTeacherWithModel:model type:type];
            }];
        }else if (model.code == 4) {
            // 取消预约(4)
            [self updateOrderTeacherWithModel:model type:1];
        } else if (model.code == 2) {
            // 评价导师
            [self evalutionTeacherWithTeacherID:self.teacherID yyid:model.yyid];
        } else if (model.code == 1) {
            // 查看评价
            [self viewEvalutionWithTeacherID:self.teacherID yyid:model.yyid];
        } else if (model.code == 6) {
            // 导师取消
            [self showCancelReasonWithyyid:model.yyid];
        }
    }
}
@end
